<?php
namespace app\admin\model;
use app\common\model\CommonModel;
use app\common\model\OrderActivity;
use app\common\model\OrderGoods;
use app\common\model\Category;
use app\common\model\Order;
use think\Db;

class OrderModel extends CommonModel
{
     /**
    * 交易管理
    */
    public static function getOrderGoods($info)
    {
        /*门店*/
		if($info['shop_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['shop_id'] = $info['shop_id'];
		}
        /*时间区间*/
        if($info['start'] != '' || $info['end'] != '')
		{
			if($info['start'])
			{
				$where['o.addtime'][] = ['>',strtotime($info['start'])];
			}
			if($info['end'])
			{
				$where['o.addtime'][] = ['<',strtotime($info['end'])];
			}
		}else{
            /*上月、当前月*/
            if(array_key_exists('shijian',$info))
            {
                if($info['shijian'] != '')
                {
                    if($info['shijian'] == CommonModel::NUMBER_ZERO)
                    {
                        $where['o.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
                        $where['o.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
                    }else{
                        $where['o.addtime'][] = ['>',mktime(0, 0, 0,date('m')-1,1,date('Y'))];
                        $where['o.addtime'][] = ['<',mktime(0, 0, 0,date('m'),0,date('Y'))];
                    }
                }
            }else{
                $where['o.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
                $where['o.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
            }
        }

        $field = 'g.goods_id,g.goods_name,g.category_id,g.cat_id,g.brand_name,g.memory_name,g.attribute_name,g.colour_name,g.marker,g.selling_price,g.trade_price,g.unit';
        $orderList = (new Order())->gorderList($where,10,$field,'g.goods_id');

        $orderList->each(function($item, $key){
            $Category = new Category();
            $item->onumber = (new Order())->orderCount(['g.goods_id'=>$item->goods_id]);
            $item->number = (new OrderGoods())->orderGoodsNumber(['goods_id'=>$item->goods_id,'is_show'=>0],'number');
            $item->category_id = $Category->getCategoryField(['category_id'=>$item->category_id],'category_name');
            $item->cat_id = $Category->getCategoryField(['category_id'=>$item->cat_id],'category_name');
        });
        return $orderList;
    }
     /**
    * 获取订单列表
    */
    public static function getOrderList($info)
    {
        $where = self::is_where($info);
        $field = 'o.order_id,o.order_sn,o.addtime,o.username,o.phone,o.user_sn,o.clerk,o.order_money,o.stay_money';
        $orderList = (new Order())->gorderList($where,2,$field);
        
        $orderList->each(function($item, $key){
            $item->ogoods = (new OrderGoods())->orderGoodsList(['order_id'=>$item->order_id],2);
        });
        
        /*销售订单数*/
        $xsdd = self::order_count($where,'count');
        /*销售总额*/
        $xsze = self::order_count($where,'sum','o.order_money');
        /*待付款总额*/
        $dfk = self::order_count($where,'sum','o.stay_money');
        
        return [$orderList,$xsdd,$xsze,$dfk];
    }   
    /**
    * 条件整理
    */
	private function is_where($info)
	{
        /*产品属性*/
		if($info['attribute_name'] != '' && $info['attribute_name'])
		{
			$where['g.attribute_name'] = $info['attribute_name'];
		}
        /*产品品牌*/
		if($info['brand_name'] != '' && $info['brand_name'])
		{
			$where['g.brand_name'] = $info['brand_name'];
		}
        /*产品子分类*/
		if($info['cat_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['g.cat_id'] = $info['cat_id'];
		}
        /*产品分类*/
		if($info['category_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['g.category_id'] = $info['category_id'];
		}
        /*产品颜色*/
		if($info['colour_name'] != '' && $info['colour_name'])
		{
			$where['g.colour_name'] = $info['colour_name'];
		}
        /*产品内存*/
		if($info['memory_name'] != '' && $info['memory_name'])
		{
			$where['g.memory_name'] = $info['memory_name'];
		}
        /*门店*/
		if($info['shop_id'] != CommonModel::NUMBER_ZERO)
		{
			$where['shop_id'] = $info['shop_id'];
		}
        /*关键字搜索*/
		if($info['sreach'])
		{
			$where['o.order_sn'] = ['like','%'.$info['sreach'].'%'];
		}
        /*时间区间*/
        if($info['start'] != '' || $info['end'] != '')
		{
			if($info['start'])
			{
				$where['o.addtime'][] = ['>',strtotime($info['start'])];
			}
			if($info['end'])
			{
				$where['o.addtime'][] = ['<',strtotime($info['end'])];
			}
			return $where;
		}
        /*上月、当前月*/
        if(array_key_exists('shijian',$info))
        {
            if($info['shijian'] != '')
            {
                if($info['shijian'] == CommonModel::NUMBER_ZERO)
                {
                    $where['o.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
                    $where['o.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
                }else{
                    $where['o.addtime'][] = ['>',mktime(0, 0, 0,date('m')-1,1,date('Y'))];
                    $where['o.addtime'][] = ['<',mktime(0, 0, 0,date('m'),0,date('Y'))];
                }
            }
        }else{
            $where['o.addtime'][] = ['>',mktime(0,0,0,date('m'),1,date('Y'))];
            $where['o.addtime'][] = ['<',mktime(23,59,59,date('m'),date('t'),date('Y'))];
        }
		return $where;
	}
    /**
    * 统计数据
    */
    private function order_count($where,$ty,$field = '')
    {
        if($ty == 'count')
        {
           return (new Order())->orderCount($where);
        }
        return (new Order())->orderSum($where,$field);
    }
    /**
    * 订单详情
    */
    public static function orderInfo($info)
    {
        $where['order_id'] = $info['oid'];
        $order = (new Order())->orderInfo($where);
        $OrderGoods = (new OrderGoods())->orderGoodsList($where);
        $OrderActivity = (new OrderActivity())->orderActivityInfo($where);
        return [$order,$OrderGoods,$OrderActivity];
    }
}
